import { login, logout, getInfo,loginWithoutAuth } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    username: '',
    token: getToken(),
    name: '',
    telephone: '',
    email: '',
    roles: []
  }
}

const state = sessionStorage.getItem('state') ? JSON.parse(sessionStorage.getItem('state')):getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USERNAME: (state, username) => {
    state.username = username
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_TELEPHONE: (state, telephone) => {
    state.telephone = telephone
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_EMAIL: (state, email) => {
    state.email = email
  },

}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        console.log(response);
        const { data } = response
        commit('SET_TOKEN', data.token)

        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  }, 
  // 游客登录
  loginWithoutAuth({ commit }) {
    return new Promise((resolve, reject) => {
      loginWithoutAuth().then(response => {
        console.log(response);
        const { data } = response
        commit('SET_TOKEN', data.token)

        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  }, 

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        sessionStorage.setItem('token',state.token)
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { name, telephone, roles, email, username } = data
        console.log(data);

        commit('SET_NAME', name)
        commit('SET_TELEPHONE', telephone)
        commit('SET_ROLES', roles)
        commit('SET_EMAIL', email)
        commit('SET_USERNAME', username)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

